package com.yanqu.road.server.cmd.activity.rank;

import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.pb.activity.CrossActivityRankProto;
import com.yanqu.road.pb.orewaractivity.OreWarActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.orewar.CrossOreWarRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = CrossProtocol.C2_CROSS_GET_ORE_ACTIVITY_RANK,desc = "盐场活动榜单")
public class GetCrossOreActivityRankListCmd extends CrossCommand {
    private long userId;
    private int activityId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityRankProto.CrossActivityRankReqMsg reqMsg = CrossActivityRankProto.CrossActivityRankReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossActivityRankListModel rankListModel = CrossOreWarRankMgr.getCrossActivityRankListModel(activityId);
        OreWarActivityProto.OreWarActivityRankListRespMsg.Builder respMsg = OreWarActivityProto.OreWarActivityRankListRespMsg.newBuilder();
        RankProto.RankListMsg.Builder rankListMsg = RankProto.RankListMsg.newBuilder();
        respMsg.setMyRank(-1);
        if (rankListModel != null) {
            rankListMsg = rankListModel.getRankListMsg();
            respMsg.setMyRank(rankListModel.getMyRank(userId, serverId));
        }
        RankProto.RankListMsg.Builder tempMsg = RankProto.RankListMsg.newBuilder();
        int num = 0;
        for (RankProto.RankTempMsg msg : rankListMsg.getRankListList()) {
            tempMsg.addRankList(msg);
            num++;
            if (num >= ActivityMgr.getRankListShowNum()) {
                break;
            }
        }
        respMsg.setRankList(tempMsg);
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_ORE_WAR_ACTIVITY_RANK, respMsg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
